<?php

namespace App\Model\Order;

use App\Component\DeviceIdentity\DeviceUser;
use App\Component\User;
use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Core\Output;
use App\Model\Member\MemberModel;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;
use EasySwoole\Component\Di;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Redis\Redis;
use EasySwoole\RedisPool\RedisPool;

/**
 * CREATE TABLE `ss_order_task_statistics` (
 * `id` int(11) NOT NULL AUTO_INCREMENT,
 * `idx_key` char(20) NOT NULL COMMENT '唯一索引标识符',
 * `task_date` date NOT NULL COMMENT '订单日期',
 * `user_id` int(11) NOT NULL COMMENT '用户id',
 * `quantity` int(11) NOT NULL COMMENT '下单总数',
 * `finished_order_count` int(11) NOT NULL COMMENT '已退款订单数量',
 * `total_user_profit` decimal(12,2) NOT NULL COMMENT '已获得提成总额',
 * `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '创建时间',
 * `created_by` varchar(32) NOT NULL DEFAULT '' COMMENT '创建人员账号',
 * `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
 * `updated_by` varchar(32) NOT NULL DEFAULT '' COMMENT '最后更新人员账号',
 * `is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除[0:未删除，1：已删除]',
 * `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '删除时间',
 * PRIMARY KEY (`id`),
 * UNIQUE KEY ```idx_key``` (`idx_key`),
 * KEY `idx_task_date` (`task_date`),
 * KEY `idx_user_id` (`user_id`)
 * ) ENGINE=InnoDB AUTO_INCREMENT=1024 DEFAULT CHARSET=utf8mb4;
 */
/**
 * Class OrderTaskStatisticsModel
 * @property integer $id
 * @property string $idx_key
 * @property string $task_date
 * @property integer $user_id
 * @property integer $quantity
 * @property float $total_user_profit
 * @property integer $finished_order_count
 * @property string $created_at
 * @property string $created_by
 * @property string $updated_at
 * @property string $updated_by
 * @property integer $is_delete
 * @property string $deleted_at
 */
class OrderTaskStatisticsModel extends BaseModel
{

    protected $tableName = 'ss_order_task_statistics';

    protected $casts = [
        'id'                   => 'integer',
        'idx_key'              => 'string',
        'task_date'            => 'string',
        'user_id'              => 'integer',
        'quantity'             => 'integer',
        'total_user_profit'    => 'float',
        'finished_order_count' => 'integer',
        'created_at'           => 'string',
        'updated_at'           => 'string',
        'deleted_at'           => 'string',
        'is_delete'            => 'integer',
        'created_by'           => 'string',
        'updated_by'           => 'string',
    ];

    const STATISTICS_BEFORE_DAY = 10; //统计前10天
    public static function getTypeDict(?string $key = null)
    {
        return self::getMap([

        ], $key);
    }

    public function member()
    {
        return $this->hasOne(MemberModel::class, function ($query) {
            $query->where('is_delete', 0);
            return $query;
        }, 'user_id', 'id');
    }
    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('orderTaskStatisticsCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('orderTaskStatisticsCache:');
    }
}
